//
//  ShoppingCarController.m
//  ios-supplier
//
//  Created by Benlai on 15/8/3.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "ShoppingCarController.h"

#import "MemoryData.h"

@interface ShoppingCarController ()<UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;

@property (nonatomic, strong) UITableView *shoppingCarTable;
@property (nonatomic, strong) NSMutableArray *shoppingCarData;
@property (nonatomic, strong) UIView *emptyBgView;
@property (nonatomic, strong) UIButton *emptySeeBtn;
//@property (nonatomic, strong) UILabel *emptyLabel;
@end

@implementation ShoppingCarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = PanelBgColorDefault;
    self.shoppingCarData = [[NSMutableArray alloc] init];
    self.shoppingCarTable = [[UITableView alloc]initWithFrame: CGRectMake(0, TopSizeFix,self.view.frame.size.width , self.view.frame.size.height - TopSizeFix - 49) style: UITableViewStylePlain];
    self.shoppingCarTable.dataSource = self;
    self.shoppingCarTable.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.shoppingCarTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview: self.shoppingCarTable];
    [self createEmptyView];
    [self.navigationItem setLeftBarButtonItem: [self createBackView]];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.emptyBgView.hidden = [MemoryData sharedMemoryData].shoppingCarList.count <= 0 ? NO : YES;
    self.shoppingCarData = [[MemoryData sharedMemoryData].shoppingCarList mutableCopy];
    if (self.shoppingCarData.count <= 0)
    {
//        self.emptyBgView.hidden = NO;
//        [self.view bringSubviewToFront: self.emptyView];
    }
    else
    {
        [self.shoppingCarTable reloadData];
        [self cellGoodsChange];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)createEmptyView
{
    self.emptyBgView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.size.width, self.view.frame.size.height)];
    self.emptyBgView.backgroundColor = [UIColor colorFromHexValue: 0xeeeeee];
    self.emptySeeBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.emptySeeBtn.frame = CGRectMake(0, 0, 150, 35);
    self.emptySeeBtn.center = self.view.center;
    self.emptySeeBtn.layer.backgroundColor = [UIColor whiteColor].CGColor;
    [self.emptySeeBtn setTitle: @"赶紧去看看" forState: UIControlStateNormal];
    [self.emptySeeBtn setTitleColor: UIColorFromRGB(0xf15353) forState: UIControlStateNormal];
    [self.emptySeeBtn.layer setCornerRadius: self.emptySeeBtn.frame.size.height / 2];
    [self.emptySeeBtn addTarget: self action: @selector(emptySeeBtnPressed:) forControlEvents: UIControlEventTouchUpInside];
    [self.emptyBgView addSubview: self.emptySeeBtn];
    UILabel *emptyLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 40)];
    emptyLabel.center = CGPointMake(self.emptySeeBtn.center.x, self.emptySeeBtn.center.y - 50);
    emptyLabel.text = @"购物车未添加任何商品";
    emptyLabel.textColor = TextColorLight;
    emptyLabel.textAlignment = NSTextAlignmentCenter;
    [self.emptyBgView addSubview: emptyLabel];
    [self.view addSubview: self.emptyBgView];
    self.emptyBgView.hidden = YES;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.shoppingCarData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"shoppingCarCell";
    GoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[GoodsCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier cellWidth: self.view.frame.size.width];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.productInfo = self.shoppingCarData[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(void)cellGoodsChange
{
    self.emptyBgView.hidden = [MemoryData sharedMemoryData].shoppingCarList.count <= 0 ? NO : YES;
    double price = [[MemoryData sharedMemoryData] getTotalPrice];
    self.totalPriceLabel.text = [NSString stringWithFormat: @"￥%.2f", price];
    
    if (self.shoppingCarData.count != [MemoryData sharedMemoryData].shoppingCarList.count)
    {
        self.shoppingCarData = [[MemoryData sharedMemoryData].shoppingCarList mutableCopy];
        [self.shoppingCarTable reloadData];
    }
    [self.delegate shoppingCarGoodsChange];
}

- (void)emptySeeBtnPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated: YES];
}

- (UIBarButtonItem *)createBackView
{
    UIButton *leftButtonItem = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButtonItem.frame = CGRectMake(0, 0, 11, 20);
    [leftButtonItem setBackgroundImage:[UIImage imageNamed:@"icon-Back"] forState:UIControlStateNormal];
    [leftButtonItem setImageEdgeInsets: UIEdgeInsetsMake((19 - 11) / 2, 0, (19 - 11) / 2, 0)];
    [leftButtonItem addTarget:self action:@selector(leftButtonItemClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:leftButtonItem];
    return item;
}

-(void)leftButtonItemClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
